import Vue from 'vue'
import Vuex from 'vuex'
// 引入插件
import createPersistedState from 'vuex-persistedstate'
import { getUserInfo } from '@/api/user/index.js'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userInfo: {}, // 用户基本信息
    userPwd: '', // 用户密码
  },
  getters: {
    username: (state) => state.userInfo.username, // 用户名
    nickname: (state) => state.userInfo.nickname, // 昵称
    user_pic: (state) => state.userInfo.user_pic, // 用户头像
  },
  mutations: {
    // 更新token
    updateToken(state, val) {
      state.token = val
    },
    // 更新用户基本信息
    updateUserInfo(state, val) {
      state.userInfo = val
    },
    // 更新用户密码
    updateUserPwd(state, val) {
      state.userPwd = val
    },
  },
  actions: {
    // 获取用户基本资料
    async getUserInfo(context) {
      const { data: res } = await getUserInfo()
      // console.log(res)
      if (res.code === 0) {
        context.commit('updateUserInfo', res.data)
      }
    },
  },
  modules: {},
  plugins: [createPersistedState()],
})
